import 'package:flutter/cupertino.dart';
/**
 * 主播介绍页
 * page介绍
 */
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:qgchat/bloc/blocProvider.dart';
import 'package:qgchat/bloc/live/anchor.dart';
import 'package:qgchat/common/action/index.dart';
import 'package:qgchat/common/class/videoCall/index.dart';
import 'package:qgchat/common/global/global.dart';
import 'package:qgchat/http/models/allgiftModel.dart';
import 'package:qgchat/http/models/anchorModel.dart';
import 'package:qgchat/page/live/videoPage/giftBox.dart';
import 'package:qgchat/states/states_model/index.dart';
import 'package:qgchat/states/states_model/socket.dart';
import 'package:qgchat/widgets/easyRefreshWidget.dart';
import 'package:qgchat/widgets/tipsDialog.dart';
import 'package:cached_network_image/cached_network_image.dart';

class AnchorInfo extends StatefulWidget {
  AnchorInfo(this.token, {Key key}) : super(key: key);

  final String token;

  @override
  _AnchorInfoState createState() => _AnchorInfoState();
}

class _AnchorInfoState extends State<AnchorInfo>
    with SingleTickerProviderStateMixin {
  List<String> tabTitles = ["个人资料", "礼物柜", "大家评价"];
  TabController tabarController;

  AnchorBloc bloc;

  @override
  void initState() {
    super.initState();
    if (mounted) {
      tabarController = TabController(length: tabTitles.length, vsync: this);
      bloc = BlocProvider.of<AnchorBloc>(context);
      bloc.bigVInfo(widget.token);
      bloc.allgift(widget.token);
    }
  }

  @override
  void dispose() {
    super.dispose();
    tabarController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: bodyHandle(),
    );
  }

  // 点击私聊按钮
  void clickChat(anchor) {
    SocketState socketState = (state(Global.context, 'socket') as SocketState);
    socketState.goChatPage(
      context,
      id: anchor.id,
      token: anchor.memberToken,
      name: anchor.memberNickname,
      head: anchor.memberHead,
    );
  }

  // 点击礼物按钮
  void clickGift(AnchorModel anchor) {
    showModalBottomSheet(
      context: context,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(px(24)),
          topRight: Radius.circular(px(24)),
        ),
      ),
      builder: (BuildContext context) {
        return GiftBox(token: anchor.memberToken);
      },
    );
  }

  // 生成body
  Widget bodyHandle() {
    return StreamBuilder(
      stream: bloc.aInfoStream,
      builder: (BuildContext context, AsyncSnapshot snapshot) {
        if (snapshot.hasData) {
          AnchorModel anchor = (snapshot.data as AnchorModel);
          List images = deJson(anchor.anchorImg);
          if (images == null || images.length <= 0) {
            return Container(
              child: Center(
                child: CupertinoActivityIndicator(),
              ),
            );
          }
          return Stack(
            children: <Widget>[
              EasyRefresh(
                header: EasyRefreshSetting.header,
                footer: EasyRefreshSetting.footer,
                onRefresh: () async {
                  bloc.bigVInfo(widget.token);
                  bloc.allgift(widget.token);
                },
                child: ListView(
                  children: <Widget>[
                    Stack(
                      children: <Widget>[
                        // 内容
                        Container(
                          margin: EdgeInsets.only(top: px(726)),
                          color: Colors.white,
                          child: infoHandle(anchor),
                        ),
                        // 轮播
                        Container(
                          height: px(726),
                          child: Stack(
                            children: <Widget>[
                              // 轮播
                              swiperHandle(anchor),
                              // 返回按钮
                              gobackHandle(),
                            ],
                          ),
                        ),
                        // 假弧形头
                        Positioned(
                          top: px(706),
                          child: Container(
                            width: w100(context),
                            height: px(20),
                            decoration: BoxDecoration(
                                color: Colors.white,
                                borderRadius: BorderRadius.only(
                                  topLeft: Radius.circular(px(20)),
                                  topRight: Radius.circular(px(20)),
                                )),
                          ),
                        ),
                        // 头像
                        Positioned(
                          top: px(654),
                          left: px(30),
                          child: Container(
                            width: px(196),
                            height: px(196),
                            decoration: BoxDecoration(
                              color: Colors.white,
                              borderRadius: BorderRadius.circular(px(98)),
                            ),
                            padding: EdgeInsets.all(px(10)),
                            child: ClipOval(
                              child: CachedNetworkImage(
                                imageUrl: "${anchor.memberHead}",
                                fit: BoxFit.cover,
                                errorWidget: (context, url, error) {
                                  return Config.errorNetImage;
                                },
                              ),
                            ),
                          ),
                        ),
                      ],
                    )
                  ],
                ),
              ),

              // 功能按钮
              stateBuilder(context, 'user', (UserState user) {
                if (user.info.memberType == 0) {
                  return Positioned(
                    bottom: 0,
                    child: actionBtnHandle(anchor),
                  );
                } else {
                  return Container();
                }
              }),
            ],
          );
        } else {
          return Container(
            child: Center(
              child: CupertinoActivityIndicator(),
            ),
          );
        }
      },
    );
  }

  // 生成功能按键
  Widget actionBtnHandle(AnchorModel anchor) {
    // 收藏按钮
    Widget heart = Container(
      width: px(80),
      height: px(80),
      margin: EdgeInsets.only(right: px(30)),
      child: StreamBuilder(
        stream: bloc.followStream,
        initialData: anchor.anchorStatus,
        builder: (BuildContext context, AsyncSnapshot snapshot) {
          if (snapshot.data == 1) {
            return RaisedButton(
              onPressed: () => bloc.cancelfollow(),
              color: Colors.white,
              padding: EdgeInsets.all(0),
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(
                  px(40),
                ),
              ),
              child: Image.asset(
                "assets/images/icon/heart_red.png",
                width: px(44),
                height: px(40),
              ),
            );
          } else {
            return RaisedButton(
              onPressed: () => bloc.follow(),
              color: Colors.white,
              padding: EdgeInsets.all(0),
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(
                  px(40),
                ),
              ),
              child: Image.asset(
                "assets/images/icon/heart.png",
                width: px(44),
                height: px(40),
              ),
            );
          }
        },
      ),
    );

    // 私聊按钮
    Widget call = Container(
      width: px(80),
      height: px(80),
      margin: EdgeInsets.only(right: px(30)),
      child: RaisedButton(
        onPressed: () {
          clickChat(anchor);
        },
        color: Colors.white,
        padding: EdgeInsets.all(0),
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(
            px(40),
          ),
        ),
        child: Image.asset(
          "assets/images/icon/call.png",
          width: px(44),
          height: px(44),
        ),
      ),
    );

    // 礼物按钮
    Widget gift = Container(
      width: px(80),
      height: px(80),
      margin: EdgeInsets.only(right: px(40)),
      child: RaisedButton(
        onPressed: () => clickGift(anchor),
        color: Colors.white,
        padding: EdgeInsets.all(0),
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(
            px(40),
          ),
        ),
        child: Image.asset(
          "assets/images/icon/gift.png",
          width: px(44),
          height: px(44),
        ),
      ),
    );

    // 视频按钮
    Widget video = Expanded(
      child: Container(
        height: px(96),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(px(48)),
        ),
        child: RaisedButton(
          onPressed: () => TipsDialog.tryVideo(context, anchor.timePrice, () {
            VideoCall.checkUserStatusAndGoVideoPage(context, widget.token);
          }),
          padding: EdgeInsets.all(0),
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(
              sp(48),
            ),
          ),
          child: Container(
            height: px(96),
            decoration: BoxDecoration(
              gradient: LinearGradient(
                colors: [col("ac70ff"), col("6f56ff")],
                begin: FractionalOffset(1, 0),
                end: FractionalOffset(0, 1),
              ),
              borderRadius: BorderRadius.circular(px(48)),
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Container(
                  margin: EdgeInsets.only(right: px(15)),
                  child: Image.asset(
                    "assets/images/icon/video.png",
                    width: px(58),
                    height: px(42),
                  ),
                ),
                Text(
                  "与Ta视频",
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: px(36),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );

    // 试看按钮
    Widget tryWatch = Expanded(
      child: Container(
        height: px(96),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(px(48)),
        ),
        child: RaisedButton(
          onPressed: () =>
              TipsDialog.tryLook(context, () => Navigator.pop(context)),
          padding: EdgeInsets.all(0),
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(
              sp(48),
            ),
          ),
          child: Container(
            height: px(96),
            decoration: BoxDecoration(
              gradient: LinearGradient(
                colors: [col("2becdd"), col("24d4f2")],
                begin: FractionalOffset(1, 0),
                end: FractionalOffset(0, 1),
              ),
              borderRadius: BorderRadius.circular(px(48)),
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Container(
                  margin: EdgeInsets.only(right: px(15)),
                  child: Image.asset(
                    "assets/images/icon/video.png",
                    width: px(58),
                    height: px(42),
                  ),
                ),
                Text(
                  "试看1分钟",
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: px(36),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );

    // 试看按钮
    Widget tryMake = Expanded(
      child: Container(
        height: px(96),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(px(48)),
        ),
        child: RaisedButton(
          onPressed: () =>
              TipsDialog.tryLook(context, () => Navigator.pop(context)),
          padding: EdgeInsets.all(0),
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(
              sp(48),
            ),
          ),
          child: Container(
            height: px(96),
            decoration: BoxDecoration(
              gradient: LinearGradient(
                colors: [col("feb445"), col("ffd563")],
                begin: FractionalOffset(1, 0),
                end: FractionalOffset(0, 1),
              ),
              borderRadius: BorderRadius.circular(px(48)),
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Container(
                  margin: EdgeInsets.only(right: px(15)),
                  child: Image.asset(
                    "assets/images/icon/video.png",
                    width: px(58),
                    height: px(42),
                  ),
                ),
                Text(
                  "预约主播",
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: px(36),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );

    return Container(
      width: w100(context),
      height: px(176),
      padding: EdgeInsets.symmetric(horizontal: px(30)),
      child: Row(
        children: <Widget>[
          heart,
          call,
          gift,
          video,
          // tryWatch,
          // tryMake,
        ],
      ),
    );
  }

  // 生成返回键
  Widget gobackHandle() {
    return Positioned(
      top: px(60),
      left: px(10),
      child: IconButton(
        onPressed: () => Navigator.pop(context),
        icon: Icon(
          Icons.navigate_before,
          color: Color.fromRGBO(0, 0, 0, 0.3),
          size: px(70),
        ),
      ),
    );
  }

  // 生成轮播
  Swiper swiperHandle(AnchorModel anchor) {
    List images = deJson(anchor.anchorImg);
    return Swiper(
      onTap: (int index) {},
      itemCount: images != null ? images.length : 0,
      itemBuilder: (BuildContext context, int index) {
        return CachedNetworkImage(
          imageUrl: "${images[index]}?imageView2/2/w/750",
          fit: BoxFit.cover,
          errorWidget: (context, url, error) {
            return Config.errorNetImage;
          },
        );
      },
      pagination: SwiperCustomPagination(
        builder: (BuildContext context, SwiperPluginConfig config) {
          return Align(
            alignment: Alignment.bottomRight,
            child: Container(
              width: px(92),
              height: px(54),
              alignment: Alignment.center,
              margin: EdgeInsets.only(right: px(30), bottom: px(50)),
              decoration: BoxDecoration(
                color: Colors.black.withOpacity(0.32),
                borderRadius: BorderRadius.circular(px(27)),
              ),
              child: Text(
                "${config.activeIndex + 1} / ${images != null ? images.length : 0}",
                style: TextStyle(
                  color: col("fefeff"),
                  fontSize: px(26),
                ),
              ),
            ),
          );
        },
      ),
    );
  }

  // 生成主播信息主要内容
  Widget infoHandle(AnchorModel anchor) {
    return Container(
      padding: EdgeInsets.only(bottom: px(176)),
      child: Column(
        children: <Widget>[
          // 头像昵称
          avatarHandle(anchor),
          // 主播数据
          dataHandle(anchor),
          // 个性签名
          signHandle(anchor),
          // 主播标签
          tagHandle(anchor),
          // 分割线
          fenge(),
          // 与我亲密
          intimateHandle(anchor),
          // 用户标签
          userTagHandle(anchor),
          // 个人资料 礼物柜 大家评价 tabbar头
          tabbarHandle(),
          // 个人资料 礼物柜 大家评价 body
          tabbarViewHandle(anchor),
        ],
      ),
    );
  }

  // 分割线
  Widget fenge() {
    return Container(
      margin: EdgeInsets.only(top: px(50)),
      child: Divider(
        indent: px(30),
        endIndent: px(30),
        color: col("edeff2"),
      ),
    );
  }

  // 头像昵称
  Widget avatarHandle(AnchorModel anchor) {
    print(anchor.memberToken);
    return Container(
      height: px(120),
      padding: EdgeInsets.only(left: px(245)),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Row(
            children: <Widget>[
              Text(
                "${anchor.memberNickname}",
                style: TextStyle(
                  color: col("303133"),
                  fontSize: px(38),
                ),
              ),
              Container(
                width: px(8),
                height: px(8),
                margin: EdgeInsets.only(left: px(20), right: px(5)),
                decoration: BoxDecoration(
                  color: CommonAction.checkAnchorStatus(
                    anchor.disturb,
                    anchor.online,
                  ).color,
                  borderRadius: BorderRadius.circular(px(4)),
                ),
              ),
              Text(
                "${CommonAction.checkAnchorStatus(
                  anchor.disturb,
                  anchor.online,
                ).text}",
                style: TextStyle(
                  color: col("303133"),
                  fontSize: px(22),
                ),
              ),
            ],
          ),
          Container(
            margin: EdgeInsets.only(top: px(20)),
            child: Row(
              children: <Widget>[
                Container(
                  height: px(34),
                  padding: EdgeInsets.symmetric(horizontal: px(15)),
                  margin: EdgeInsets.only(right: px(20)),
                  decoration: BoxDecoration(
                    color: col("ffb21c"),
                    borderRadius: BorderRadius.circular(px(17)),
                  ),
                  child: Row(
                    children: <Widget>[
                      Container(
                        margin: EdgeInsets.only(right: px(9)),
                        child: Image.asset(
                          "assets/images/icon/king.png",
                          width: px(20),
                          height: px(18),
                        ),
                      ),
                      Text(
                        "Lv${anchor.anchorStarts}",
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: px(22),
                        ),
                      )
                    ],
                  ),
                )
              ],
            ),
          )
        ],
      ),
    );
  }

  // 生成主播数据
  Widget dataHandle(AnchorModel anchor) {
    // 单个数据样式
    Widget dataStyle(count, title) {
      return Container(
        width: px(210),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: <Widget>[
            Text(
              "$count",
              style: TextStyle(
                height: .9,
                color: col("303133"),
                fontSize: px(40),
              ),
            ),
            Text(
              title,
              style: TextStyle(
                color: col("909399"),
                fontSize: px(28),
              ),
            ),
          ],
        ),
      );
    }

    // 分割线
    Widget shuStyle = Container(
      width: 1,
      height: px(50),
      color: col("edeff2"),
    );

    return Container(
      height: px(80),
      margin: EdgeInsets.only(top: px(60)),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          dataStyle(anchor.receiveCount, "已视频"),
          shuStyle,
          dataStyle(anchor.anchorFans, "粉丝"),
          shuStyle,
          dataStyle(anchor.timePrice, "时光价值"),
        ],
      ),
    );
  }

  // 个性签名
  Widget signHandle(AnchorModel anchor) {
    return Container(
      margin: EdgeInsets.only(top: px(40)),
      padding: EdgeInsets.symmetric(horizontal: px(30)),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Container(
            margin: EdgeInsets.only(right: px(15), top: px(20)),
            child: Text(
              "Ta说",
              style: TextStyle(
                color: col("5640c6"),
                fontSize: px(28),
              ),
            ),
          ),
          Expanded(
            child: Container(
              padding:
                  EdgeInsets.symmetric(horizontal: px(30), vertical: px(20)),
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(px(22)),
                color: col("f5f7fa"),
              ),
              child: Text(
                "${anchor.anchorSign}",
                style: TextStyle(
                  color: col("303133"),
                  fontSize: px(28),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  // 主播标签
  Widget tagHandle(AnchorModel anchor) {
    tag(color, title) {
      return Container(
        margin: EdgeInsets.only(right: px(15)),
        padding: EdgeInsets.symmetric(horizontal: px(20), vertical: px(10)),
        decoration: BoxDecoration(
          color: col(color),
          borderRadius: BorderRadius.circular(px(22)),
        ),
        child: Text(
          title,
          style: TextStyle(
            color: Colors.white,
            fontSize: px(24),
          ),
        ),
      );
    }

    return Container(
      margin: EdgeInsets.only(top: px(20)),
      padding: EdgeInsets.only(right: px(30), left: px(107)),
      alignment: Alignment.centerLeft,
      child: Row(
        mainAxisSize: MainAxisSize.min,
        children: anchor.tags.map<Widget>((Tags item) {
          return tag("${item.tagcolor.replaceAll('#', '')}", "${item.tagName}");
        }).toList(),
      ),
    );
  }

  // 与我亲密
  Widget intimateHandle(AnchorModel anchor) {
    List<Closeman> closeman = [];
    for (Closeman item in anchor.closeman) {
      closeman.add(item);
    }
    if (closeman.length > 3) {
      closeman.removeRange(3, closeman.length);
    }

    // 标题
    Widget title = Container(
      height: px(90),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          Text(
            "与我亲密的",
            style: TextStyle(
              color: col("303133"),
              fontSize: px(32),
              fontWeight: FontWeight.bold,
            ),
          ),
          Icon(
            Icons.navigate_next,
            color: col("606266"),
            size: px(52),
          ),
        ],
      ),
    );

    // 亲密图标
    Widget intimate = Container(
      width: w100(context),
      height: px(88),
      child: Stack(
        children: closeman.asMap().keys.map<Widget>(
          (int i) {
            return Positioned(
              left: px(i.toDouble() * 70.0),
              child: Container(
                width: px(88),
                height: px(88),
                padding: EdgeInsets.all(px(5)),
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(px(44)),
                  color: Colors.white,
                ),
                child: ClipOval(
                  child: CachedNetworkImage(
                    imageUrl: "${closeman[i].memberHead}",
                    fit: BoxFit.cover,
                    errorWidget: (context, url, error) {
                      return Config.errorNetImage;
                    },
                  ),
                ),
              ),
            );
          },
        ).toList()
          ..add(
            Positioned(
              left: px(closeman.length.toDouble() * 70.0),
              child: Container(
                width: px(88),
                height: px(88),
                alignment: Alignment.center,
                decoration: BoxDecoration(
                  border: Border.all(
                    color: Colors.white,
                    width: px(5),
                  ),
                  borderRadius: BorderRadius.circular(px(44)),
                  color: col("5640c6"),
                ),
                child: Text(
                  "${anchor.closeman.length}",
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: px(32),
                  ),
                ),
              ),
            ),
          ),
      ),
    );

    return Container(
      padding: EdgeInsets.symmetric(horizontal: px(30)),
      child: FlatButton(
        onPressed: () => goPage(context, "/ranking", param: {
          "memberToken": anchor.memberToken,
        }),
        padding: EdgeInsets.zero,
        child: Column(
          children: <Widget>[
            title,
            intimate,
          ],
        ),
      ),
    );
  }

  // 用户标签
  Widget userTagHandle(AnchorModel anchor) {
    // 标题
    Widget title = Container(
      height: px(90),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          Text(
            "用户标签",
            style: TextStyle(
              color: col("303133"),
              fontSize: px(32),
              fontWeight: FontWeight.bold,
            ),
          ),
          Icon(
            Icons.navigate_next,
            color: col("606266"),
            size: px(52),
          ),
        ],
      ),
    );

    // 标签
    Widget tag = Container(
      width: w100(context),
      child: Row(
        mainAxisSize: MainAxisSize.min,
        children: anchor.xingxiangq.map<Widget>((Xingxiangq item) {
          return Container(
            margin: EdgeInsets.only(right: px(15)),
            padding: EdgeInsets.symmetric(horizontal: px(20), vertical: px(12)),
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(px(23)),
              color: col("${item.xingxiangcolor.replaceAll('#', '')}"),
            ),
            child: Text(
              "${item.usertagName}",
              style: TextStyle(
                height: 0.99,
                color: Colors.white,
                fontSize: px(24),
              ),
            ),
          );
        }).toList(),
      ),
    );

    return Container(
      margin: EdgeInsets.only(top: px(20)),
      padding: EdgeInsets.symmetric(horizontal: px(30)),
      child: FlatButton(
        onPressed: () => goPage(context, "/allTag", param: {'id': anchor.id}),
        padding: EdgeInsets.zero,
        child: Column(
          children: <Widget>[
            title,
            tag,
          ],
        ),
      ),
    );
  }

  // 个人资料 礼物柜 大家评价 tabbar头
  Widget tabbarHandle() {
    return Container(
      width: w100(context),
      margin: EdgeInsets.only(top: px(80)),
      decoration: BoxDecoration(
        border: Border(
          bottom: BorderSide(
            color: col("edeff2"),
            width: 1,
          ),
        ),
      ),
      child: TabBar(
        isScrollable: true,
        controller: tabarController,
        labelColor: col("303133"),
        labelPadding: EdgeInsets.symmetric(horizontal: px(30)),
        labelStyle: TextStyle(
          fontWeight: FontWeight.bold,
        ),
        unselectedLabelStyle: TextStyle(fontSize: px(32)),
        unselectedLabelColor: col("909399"),
        indicatorColor: col("303133"),
        indicatorSize: TabBarIndicatorSize.label,
        indicatorWeight: px(4),
        tabs: tabTitles.map<Tab>((item) {
          return Tab(text: item);
        }).toList(),
      ),
    );
  }

  // 个人资料 礼物柜 大家评价 body
  Widget tabbarViewHandle(AnchorModel anchor) {
    return Container(
      width: w100(context),
      height: px(550),
      child: TabBarView(
        controller: tabarController,
        children: [
          userInfoHandle(anchor),
          _GiftHandle(
            bloc: bloc,
            token: widget.token,
          ),
          evaluateHandle(anchor),
        ],
      ),
    );
  }

  // 个人资料
  userInfoHandle(AnchorModel anchor) {
    Widget options(String key, String val) {
      return Container(
        margin: EdgeInsets.only(top: px(30)),
        child: Row(
          children: <Widget>[
            Container(
              width: px(137),
              child: Text(
                key,
                style: TextStyle(
                  color: col("909399"),
                  fontSize: px(28),
                ),
              ),
            ),
            Text(
              val,
              style: TextStyle(
                color: col("303133"),
                fontSize: px(28),
              ),
            ),
          ],
        ),
      );
    }

    return Container(
      padding: EdgeInsets.symmetric(horizontal: px(30), vertical: px(5)),
      child: Column(
        children: <Widget>[
          options("城市", "${anchor.anchorCity}"),
          options("身高", "${anchor.anchorHeight}"),
          options("体重", "${anchor.anchorWeight}"),
          options("星座", "${anchor.anchorStar}"),
          options("接听率", "${anchor.rate}%"),
          options("最后登录", "${anchor.lasttime}"),
        ],
      ),
    );
  }

  // 大家评价
  Widget evaluateHandle(AnchorModel anchor) {
    Widget buildTag(TagLabel tag) {
      return Container(
        margin: EdgeInsets.only(left: px(15)),
        padding: EdgeInsets.symmetric(horizontal: px(20), vertical: px(12)),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(px(23)),
          color: col("${tag.xingxiangcolor.replaceAll('#', '')}"),
        ),
        child: Text(
          "${tag.usertagName}",
          style: TextStyle(
            height: 0.99,
            color: Colors.white,
            fontSize: px(24),
          ),
        ),
      );
    }

    // 单个用户评价
    Widget userEvaluate(Allcomment item) {
      return Container(
        height: px(160),
        child: Row(
          children: <Widget>[
            Container(
              width: px(88),
              height: px(88),
              child: ClipOval(
                child: CachedNetworkImage(
                  imageUrl: '${item.memberHead}',
                  fit: BoxFit.cover,
                  errorWidget: (context, url, error) {
                    return Config.errorNetImage;
                  },
                ),
              ),
            ),
            Container(
              width: px(143),
              padding: EdgeInsets.symmetric(horizontal: px(20)),
              child: Text(
                "${item.memberNickname}",
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
                style: TextStyle(
                  color: col("303133"),
                  fontSize: px(28),
                ),
              ),
            ),
            Expanded(
              child: Row(
                  children: item.tagLabel.map<Widget>((TagLabel tag) {
                return buildTag(tag);
              }).toList()),
            )
          ],
        ),
      );
    }

    return Container(
      padding: EdgeInsets.symmetric(horizontal: px(30)),
      child: ListView(
        padding: EdgeInsets.zero,
        children: anchor.allcomment.map<Widget>((item) {
          return userEvaluate(item);
        }).toList(),
      ),
    );
  }
}

class _GiftHandle extends StatefulWidget {
  _GiftHandle({this.bloc, this.token, Key key}) : super(key: key);

  final AnchorBloc bloc;
  final String token;

  @override
  _GiftHandleState createState() => _GiftHandleState();
}

class _GiftHandleState extends State<_GiftHandle>
    with AutomaticKeepAliveClientMixin {
  @override
  bool get wantKeepAlive => true;

  @override
  void initState() {
    super.initState();
    if (mounted) {
      widget.bloc.allgift(widget.token);
    }
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Container(
      width: w100(context),
      padding: EdgeInsets.symmetric(horizontal: px(30)),
      child: StreamBuilder(
        stream: widget.bloc.giftStream,
        builder: (BuildContext context, AsyncSnapshot snapshot) {
          if (snapshot.hasData) {
            AllgiftModel allgitf = (snapshot.data as AllgiftModel);
            return ListView(
              padding: EdgeInsets.zero,
              children: <Widget>[
                giftCount(allgitf.sum),
                giftList(allgitf.datalist),
              ],
            );
          } else {
            return Container();
          }
        },
      ),
    );
  }

  // 礼物总数
  Widget giftCount(int sum) {
    return Container(
      margin: EdgeInsets.only(top: px(50)),
      child: RichText(
        text: TextSpan(
          text: "礼物总数：",
          style: TextStyle(
            color: col("909399"),
            fontSize: px(28),
          ),
          children: <TextSpan>[
            TextSpan(
              text: "$sum",
              style: TextStyle(
                color: col("303133"),
                fontSize: px(32),
                fontWeight: FontWeight.bold,
              ),
            )
          ],
        ),
      ),
    );
  }

  // 礼物
  Widget gift(Datalist item) {
    return Container(
      child: Column(
        children: <Widget>[
          Container(
            width: px(111),
            height: px(111),
            alignment: Alignment.center,
            decoration: BoxDecoration(
              color: col("f8f8f8"),
            ),
            child: CachedNetworkImage(
              imageUrl: '${item.icon}',
              width: px(100),
              height: px(100),
              fit: BoxFit.cover,
              errorWidget: (context, url, error) {
                return Image.asset(
                  "assets/images/icon/giftbg.png",
                  width: px(68),
                  height: px(67),
                );
              },
            ),
          ),
          Container(
            margin: EdgeInsets.only(top: px(20)),
            child: Text(
              "${item.name}",
              style: TextStyle(
                height: 0.99,
                color: col("303133"),
                fontSize: px(28),
              ),
            ),
          ),
          Container(
            margin: EdgeInsets.only(top: px(15)),
            child: Text(
              "x${item.num}",
              style: TextStyle(
                height: 0.99,
                color: col("5640c6"),
                fontSize: px(26),
              ),
            ),
          ),
        ],
      ),
    );
  }

  // 礼物列表
  Widget giftList(List<Datalist> datalist) {
    return Container(
      margin: EdgeInsets.only(top: px(40)),
      child: Wrap(
        spacing: px(33),
        runSpacing: px(20),
        children: datalist.map<Widget>((Datalist item) {
          return gift(item);
        }).toList(),
      ),
    );
  }
}
